package br.com.yahoo.mau_mss.tutorjavafxgolfx.controller;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 * <p>Title: TesteProgressoApp</p>
 * <p>Description:  </p>
 * <p>Date: Oct 16, 2013, 1:46:59 PM</p>
 * @author Mauricio Soares da Silva (mauricio.soares)
 */
public class TesteProgressoApp extends Application {
  private AnchorPane pane;
  private ProgressIndicator progress;
  private static Stage stage;

  /**
   * Create a new instance of <code>TesteProgressoApp</code>.
   */
  public TesteProgressoApp() {
  }

  @Override
  public void start(Stage stage) throws Exception {
    initComponents();
    initTask();
		Scene scene = new Scene(pane);
		stage.setScene(scene);
		stage.setTitle("Tela com Progress Indicator");
		stage.show();
    TesteProgressoApp.stage = stage;
  }
  
  private void initComponents() {
    this.pane = new AnchorPane();
    this.pane.setPrefSize(400, 200);
		this.progress = new ProgressIndicator(0.0);
		this.pane.getChildren().add(progress);
  }
  
  private void initTask() {
		Task<Void> task = new Task<Void>() {
			@Override
			protected Void call() throws Exception {
				final int max = 10;
				for (int i = 1; i <= max; i++) {
					updateProgress(i, max);
					try {
						Thread.sleep(1000);
					} catch (Exception e) {
						Logger.getLogger(LoginApp.class.getName()).log(Level.SEVERE, null, e);
					}
				}
				JOptionPane.showMessageDialog(null, "Progresso concluído!");
				return null;
			}
		};
		this.progress.progressProperty().bind(task.progressProperty());
		new Thread(task).start();
  }

}